<?php

namespace app\admin\controller;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Controller;
use think\Request;

class Materiallist extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = [];
        $list = db("eb_special_task")->where($where)->paginate(5, false, ['query' => request()->param()]);
        $data = $list->items();
        $this->assign('list', $list);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if (request()->isPost()) {
            $inputdata = input();
            $data = array();
            $data['title'] = $inputdata['title'];
            $data['sort'] = $inputdata['sort'];
            $data['image'] = $inputdata['image'];
            $data['detail'] = $inputdata['detail'];
            $data['content'] = $inputdata['content'];
            // $materiallistModel = model("Materiallist");
            //   $rat = $materiallistModel->MateriallistAdd($data);
            $res = db("eb_special_task")->insert($data);
            if ($res) {
                $this->success('添加成功', url('Materiallist/index'));
            } else {
                $this->error('添加失败');
            }
        } else {
            return $this->fetch();
        }
    }

    public function save_ima_remote()
    {
        $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = "hV1J1JA6b4OKqpJL9yhPqUyX8PrgL-jfwLoRjS5Q";
        $secretKey = "OnRgqLeyHEb9cfm1aifxCacgWRKV6y_l_fQS-m1k";
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = "wanmgsiyi";

        //域名
        $domain = "quu2xz1i9.hn-bkt.clouddn.com";
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 1, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $usercatemodel = model("Materiallist");
        $find = $usercatemodel->getOne($id);
        $this->assign('find',$find);
        return $this->fetch();
}


    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $where = [];
        $where['id']= $id;
        $inpudata = input();
        $usercatemodel = model("Materiallist");
        $res = $usercatemodel->Materiallistupdate($where,$inpudata);
        if ($res){
            return $this->success('修改成功',url('Materiallist/index'));
        }else{
            return $this->error('修改失败');
        }
    }

    /**
     * 编辑简介
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function info($id){
        $usercatemodel = model("Materiallist");
        $find = $usercatemodel->getInfo($id);
        $this->assign('find',$find);
        return $this->fetch();
    }
    public function infoedit(Request $request, $id){
        $where = [];
        $where['id']= $id;
        $inpudata = input();
        $usercatemodel = model("Materiallist");
        $res = $usercatemodel->Infoeditupdate($where,$inpudata);
        if ($res){
            return $this->success('修改成功',url('Materiallist/index'));
        }else{
            return $this->error('修改失败');
        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id= input('id');
        if (db('eb_special_task')->where('id',$id)->delete()) {
            $this->success('删除成功',url("Materiallist/index"));
        } else {
            $this->error('删除失败');
        }
    }
}
